<?php

namespace app\admin\controller;

use app\admin\model\ActivityCatgory;
use app\admin\model\ActivityClass;
use app\admin\model\QuestionBank;
use app\services\Oss;
use think\Config;
use think\Controller;
use think\Request;

class QuestionBankController extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $banks = QuestionBank::paginate(10);
        return view('index',compact('banks'));
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add(Request $request)
    {
        //
        $categories = ActivityCatgory::all();
        $class = ActivityClass::all();
        if ($request->isPost()) {
            $data = $request->post();
            $file = $request->file("image");
            if ($file) {
                $url = $this->upload($file);
                $data['thumb'] = $url;
            }
            $validate = $this->validate($data, [
                'title|题库名称' => 'require',
            ]);
            if ($validate !== true) {
                //验证没有通过
                return $this->error($validate);
            }
            $data['addtime'] = time();
            $bank = new QuestionBank();
            $res = $bank->insert($data);
            if ($res) {
                return $this->success('添加成功', 'index');
            } else {
                return $this->error('添加失败', 'add');
            }
        }
        return view('add',compact('class','categories'));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read(Request $request,$id)
    {
        //

    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(Request $request,$id)
    {
        //
        $categories = ActivityCatgory::all();
        $class = ActivityClass::all();
        $bank = QuestionBank::get($id);
        if ($request->isPost()) {
            $data = $request->post();
            $file = $request->file("image");
            if ($file) {
                $url = $this->upload($file);
                $old_url = $bank->thumb;
                if ($old_url != ""){
                    $old_url = substr($old_url,strripos($old_url,"/")+1);
                    $config = Config::get('aliyun_oss');
                    Oss::publicDeleteObject($config['Bucket'],$old_url);
                }
                $data['thumb'] = $url;
            }
            $validate = $this->validate($data, [
                'title|题库名称' => 'require',
            ]);
            if ($validate !== true) {
                //验证没有通过
                return $this->error($validate);
            }
            $res = $bank->allowField(true)->save($data);
            if ($res) {
                return $this->success('修改成功', 'index');
            } else {
                return $this->error('修改失败', 'edit');
            }
        }
        return view('edit',compact('class','categories','bank'));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    public function upload($file)
    {
        $config = Config::get('aliyun_oss');

        $video=$file->getRealPath();
        $fileName = $file->getInfo()['name'];
        $fileType = substr($fileName,strripos($fileName,".")+1);
        $key = time() . rand(10000, 99999999).".".$fileType;
        $res = Oss::publicUpload($config['Bucket'], $key,$video);
        if ($res){
            $url = 'https://'.$config['Bucket'].'.'.$config['Endpoint'].'/'.$key;
        }
        return $url;
    }
}
